package com.bupt.kgplatform.controller.docker;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bupt.kgplatform.Util.GLMutil;
import com.bupt.kgplatform.Util.GetNowTime;
import com.bupt.kgplatform.Util.R;
import com.bupt.kgplatform.Util.ResultCode;
import com.bupt.kgplatform.entity.GptTask;
import com.bupt.kgplatform.mapper.GptTaskMapper;
import com.bupt.kgplatform.service.GptTaskService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * DockerController
 *
 * @author zrz
 * @date 2023/5/26
 * @time 16:35
 * @Description docker
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("/gptTask" )
public class GptTaskController {


    private final GptTaskService gptTaskService;

    @Autowired(required = false)
    private final GptTaskMapper gptTaskMapper;

    @Value("${host.ip}")
    public String ip;

    @Autowired(required = false)
    private GetNowTime getNowTime;

    @PostMapping("/add")
    public R addTask(@RequestBody GptTask gptTask){
        System.out.println(gptTask);
        System.out.println(getNowTime.getStringTime());
        gptTask.setCreateTime(getNowTime.getStringTime());
        int insert = gptTaskMapper.insert(gptTask);
        if(insert == 1){
            return R.ok(ResultCode.SUCCESS, "添加成功");
        }
        else{
            return R.failure(ResultCode.FAILURE, "添加失败");
        }
    }

    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable String id) {
        boolean b = gptTaskService.removeById(id);
        if(b){
            return R.ok(ResultCode.SUCCESS, "删除成功");
        }
        else{
            return R.failure(ResultCode.FAILURE, "删除失败");
        }

    }

    @GetMapping("/getAll")
    public R getAll(@Param("current") Integer current, @Param("size") Integer size){
        Page<GptTask> page = new Page<>(current, size);
        Page<GptTask> gptTaskPage = gptTaskMapper.selectPage(page, null);
        return R.ok(gptTaskPage);
    }

    @PostMapping("/updateById")
    public R updateById(@RequestBody GptTask gptTask){
        int i = gptTaskMapper.updateById(gptTask);
        if(i == 1){
            return R.ok(ResultCode.SUCCESS, "更新成功");
        }
        else{
            return R.failure(ResultCode.FAILURE, "更新失败");
        }
    }

    @Resource
    private GLMutil glMutil;

    @GetMapping("/goToChat")
        public R DD(@Param("gptTaskId") Integer gptTaskId){
        boolean b = glMutil.main1(gptTaskId);
        if(b){
            return R.ok();
        }else{
            return R.failure(ResultCode.FAILURE,"启动失败");
        }

    }



}
